package jwdt.wizards;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import jwdt.Activator;
import jwdt.common.Constant;
import jwdt.popup.actions.DevToolsAction;
import jwdt.popup.actions.InitProFrameWorkAction;
import jwdt.utils.JWDTSettingUtils;
import jwdt.utils.VelocityUtils;
import jwdt.vo.InitProDataSourceVo;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWizard;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.osgi.framework.Bundle;

public class InitProFrameWorkWizard extends Wizard implements INewWizard {
	private InitProFrameWorkWizardPage_1 page1;
	private InitProFrameWorkWizardPage_2 page2;
	private ISelection selection;

	/**
	 * Constructor for InitProFrameWorkWizard.
	 */
	public InitProFrameWorkWizard(ISelection selection) {
		super();
		setNeedsProgressMonitor(true);
		this.selection = selection;
	}
	
	/**
	 * Adding the page to the wizard.
	 */

	public void addPages() {
		page1 = new InitProFrameWorkWizardPage_1(selection);
		addPage(page1);
		page2 = new InitProFrameWorkWizardPage_2(selection);		
		addPage(page2);
	}

	/**
	 * This method is called when 'Finish' button is pressed in
	 * the wizard. 
	 */
	public boolean performFinish() {
		if(InitProFrameWorkAction.projectProperties.isInitProFrameWork()){
			if(!MessageDialog.openConfirm(null, "提示信息", "该项目已做过初始化，是否继续...")){
				return true;
			}
		}
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {					
					//setp1 : 初始化项目结构
					initProject(monitor);
					
					//setp2 : 根据配置加载jar文件
					loadJars(monitor);
					
					//setp3 : 根据配置初始化配置文件
					loadConfigs(monitor);
					
					//setp4 : 生成基础包和文件
					loadFiles(monitor);
					
					//setp5 : 生成基础页面
					IFolder ifolder = InitProFrameWorkAction.javaProject.getProject().getFolder("/" + InitProFrameWorkAction.projectProperties.getWebappPath());
					loadPageBase(Constant.PAGE_BASE,ifolder,monitor);					
					
					//setp6 : 生成SYS_MENU功能
					loadSysMenu(monitor);
					
					//setp7 : 生成.jwdt.initproframework
					JWDTSettingUtils.saveSettings(InitProFrameWorkAction.projectProperties.getProjectPath(), page1.getFws(), page2.getInitProDataSourceVoMap());
					
					//setp8 : 设置项目编码为UTF-8
					JWDTSettingUtils.saveEncodingSettings(InitProFrameWorkAction.projectProperties.getProjectPath());
					
					// 刷新项目
					InitProFrameWorkAction.javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
				} catch (Exception e) {					
					MessageDialog.openError(null, "错误", e.getMessage());
				}
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
			return false;
		}		
		
		return true;
	}
	//初始化项目结构
	private void initProject(IProgressMonitor monitor) throws Exception{
		IJavaProject javaProject=InitProFrameWorkAction.javaProject;
		IProject project = javaProject.getProject();
		
		if(InitProFrameWorkAction.projectProperties.isInitProFrameWork()){
			return;
		}
		
		//src_model
		IFolder src_model=project.getFolder("src_"+InitProFrameWorkAction.projectProperties.getProjectName().toLowerCase());
		if(!src_model.exists()){
			src_model.create(true, true, monitor);
			
			IClasspathEntry[] classpathEntrys = javaProject.getRawClasspath();					
			IClasspathEntry[] newClasspathEntrys=new IClasspathEntry[classpathEntrys.length+1];					
			for(int i=0;i<classpathEntrys.length;i++){
				if(i==0){
					newClasspathEntrys[i]=classpathEntrys[i];
					newClasspathEntrys[i+1]=JavaCore.newSourceEntry(src_model.getFullPath());
				}else{
					newClasspathEntrys[i+1]=classpathEntrys[i];
				}											
			}
			
			javaProject.setRawClasspath(newClasspathEntrys, monitor);
		}
				
		
		//重新设置OutputLocation，删除旧的
		IFolder newOutputLocation=project.getFolder(InitProFrameWorkAction.projectProperties.getWebappPath()+"/WEB-INF/classes");
		if(!newOutputLocation.exists()){
			newOutputLocation.create(true, true, monitor);	
			
			javaProject.setOutputLocation(newOutputLocation.getFullPath(), monitor);	
		}						
		
		
		IFolder buildIFolder = project.getFolder("build");
		if(buildIFolder.exists()){
			buildIFolder.delete(true, monitor);	
		}
		
		//dbscript
		IFolder dbscript=project.getFolder("dbscript");
		if(!dbscript.exists()){
			dbscript.create(true, true, monitor);
		}
	}
	// 根据选择加载jar文件
	private void loadJars(IProgressMonitor monitor) throws CoreException,IOException {
		//libs
		Map<String, String> libs = new HashMap<String, String>();
		
		//page1
		for (String key : page1.getFws().keySet()) {
			String fwValue = page1.getFws().get(key);
			Map<String, String> rules = Constant.INITPROFRAMEWORK_RESOURCE_RULE_MAP.get(fwValue);
			if(rules != null){
				libs.putAll(rules);
			}			
		}
		//page2
		if(page2.getInitProDataSourceVoMap().size() > 1){
			Map<String, String> rules = Constant.INITPROFRAMEWORK_RESOURCE_RULE_MAP.get(Constant.MULTIPLE_DATASOURCE);
			if(rules != null){
				libs.putAll(rules);
			}
		}
		if(page2.getInitProDataSourceVoMap().size() == 1){
			Map<String, String> rules = Constant.INITPROFRAMEWORK_RESOURCE_RULE_MAP.get(Constant.SINGLE_DATASOURCE);
			if(rules != null){
				libs.putAll(rules);
			}
		}
		for(String key : page2.getInitProDataSourceVoMap().keySet()){
			InitProDataSourceVo initProDataSourceVo = page2.getInitProDataSourceVoMap().get(key);			
			Map<String, String> rules = Constant.INITPROFRAMEWORK_RESOURCE_RULE_MAP.get(initProDataSourceVo.getDataBaseType());
			if(rules != null){
				libs.putAll(rules);
			}
		}
		
		//create libs
		Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
		IFolder ifolder = InitProFrameWorkAction.javaProject.getProject()
				.getFolder("/" + InitProFrameWorkAction.projectProperties.getWebappPath() + "/WEB-INF/lib");
		for (String key : libs.keySet()) {
			System.out.println("key:"+key);
			URL url = bundle.getResource(libs.get(key));
			System.out.println("url:"+url);
			InputStream is = url.openStream();

			IFile ifile = ifolder.getFile(key);
			if(!ifile.exists()){
				ifile.create(is, true, monitor);
			}			
		}		
	}
	//根据配置初始化配置文件
	private void loadConfigs(IProgressMonitor monitor) throws CoreException,IOException {
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		/*
		 * web.xml
		 */
		if(Constant.MVC_STRUTS.equals(page1.getFws().get(Constant.MVC))){
			
		}else if(Constant.MVC_STRUTS2.equals(page1.getFws().get(Constant.MVC))){
			templateFile = "templates/initproframework/web2.vm";
		}else{
			templateFile = "templates/initproframework/web.vm";
		}		
		outFilePath = InitProFrameWorkAction.projectProperties.getProjectPath()+"/"+InitProFrameWorkAction.projectProperties.getWebappPath()+"/WEB-INF/web.xml";
		parameters.clear();
		parameters.put("moduleVersion", InitProFrameWorkAction.projectProperties.getModuleVersion());
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
		/*
		 * config
		 */
		String srcPath = InitProFrameWorkAction.projectProperties.getProjectPath()+"/"+InitProFrameWorkAction.projectProperties.getJavaSourcesPath()[0];
		String outPath = InitProFrameWorkAction.projectProperties.getProjectPath()+"/"+InitProFrameWorkAction.projectProperties.getWebappPath()+"/WEB-INF/config";
		File file = new File(outPath);
		if(!file.exists()){
			file.mkdirs();
		}
		/*
		 * orm config
		 */
		String orm = page1.getFws().get(Constant.ORM);
		//ibatis
		if(orm.equals(Constant.ORM_IBATIS)){
			//SINGLE_DATASOURCE
			if(page2.getInitProDataSourceVoMap().size() == 1){						
				templateFile = "templates/initproframework/ibatis-config.vm";
				outFilePath = outPath +"/ibatis-config.xml";
				parameters.clear();
				parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
				parameters.put("initProDataSourceVo", page2.getInitProDataSourceVoMap().values().toArray()[0]);
				VelocityUtils.evaluate(templateFile, outFilePath, parameters);
			}
			//MULTIPLE_DATASOURCE
			if(page2.getInitProDataSourceVoMap().size() > 1){
				//ibatis-xa-config.xml
				templateFile = "templates/initproframework/ibatis-xa-config.vm";
				outFilePath = outPath +"/ibatis-xa-config.xml";
				parameters.clear();
				parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
				parameters.put("initProDataSourceVoMap", page2.getInitProDataSourceVoMap());
				VelocityUtils.evaluate(templateFile, outFilePath, parameters);
				//carol.properties
				templateFile = "templates/initproframework/jta.vm";
				outFilePath = srcPath +"/jta.properties";
				parameters.clear();
				VelocityUtils.evaluate(templateFile, outFilePath, parameters);
			}
			//sqlMapConfig.xml
			templateFile = "templates/initproframework/sqlMapConfig.vm";
			outFilePath = outPath +"/sqlMapConfig.xml";
			parameters.clear();
			parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
			VelocityUtils.evaluate(templateFile, outFilePath, parameters);
		}
		//hibernate
		if(orm.equals(Constant.ORM_HIBERNATE)){
			//SINGLE_DATASOURCE
			if(page2.getInitProDataSourceVoMap().size() == 1){						
				templateFile = "templates/initproframework/hibernate-config.vm";
				outFilePath = outPath +"/hibernate-config.xml";
				parameters.clear();
				parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
				parameters.put("initProDataSourceVo", page2.getInitProDataSourceVoMap().values().toArray()[0]);
				VelocityUtils.evaluate(templateFile, outFilePath, parameters);
			}
			//MULTIPLE_DATASOURCE
			if(page2.getInitProDataSourceVoMap().size() > 1){
				templateFile = "templates/initproframework/hibernate-xa-config.vm";
				outFilePath = outPath +"/hibernate-xa-config.xml";
				parameters.clear();
				parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
				parameters.put("initProDataSourceVoMap", page2.getInitProDataSourceVoMap());
				VelocityUtils.evaluate(templateFile, outFilePath, parameters);
				//carol.properties
				templateFile = "templates/initproframework/jta.vm";
				outFilePath = srcPath +"/jta.properties";
				parameters.clear();
				VelocityUtils.evaluate(templateFile, outFilePath, parameters);
			}
			//hibernate.cfg.xml
			templateFile = "templates/initproframework/hibernate.cfg.vm";
			outFilePath = srcPath +"/hibernate.cfg.xml";
			parameters.clear();
			parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
			VelocityUtils.evaluate(templateFile, outFilePath, parameters);
		}
		/*
		 * ioc config
		 */
		String ioc = page1.getFws().get(Constant.IOC);
		//spring config
		if(ioc.equals(Constant.IOC_SPRING)){
			templateFile = "templates/initproframework/model-config.vm";
			outFilePath = outPath +"/model-config.xml";
			parameters.clear();
			parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
			VelocityUtils.evaluate(templateFile, outFilePath, parameters);
		}
		/*
		 * mvc config
		 */
		String mvc = page1.getFws().get(Constant.MVC);
		//springMVC config
		if(mvc.equals(Constant.MVC_SPRINGMVC)){
			templateFile = "templates/initproframework/springMVC-servlet.vm";
			outFilePath = outPath +"/springMVC-servlet.xml";
			parameters.clear();
			parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
			VelocityUtils.evaluate(templateFile, outFilePath, parameters);
		}else if(mvc.equals(Constant.MVC_STRUTS2)){
			templateFile = "templates/initproframework/struts2.vm";
			outFilePath = srcPath +"/struts.xml";
			parameters.clear();
			parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
			VelocityUtils.evaluate(templateFile, outFilePath, parameters);
			
			//message.properties
			outFilePath = srcPath +"/message.properties";
			File outFile = new File(outFilePath);
			if(!outFile.exists()){
				outFile.createNewFile();
			}
		}
		
		/*
		 * public config
		 */
		//log
		templateFile = "templates/initproframework/log4j.vm";
		outFilePath = srcPath +"/log4j.xml";
		parameters.clear();
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	
	//生成基础包和文件
	private void loadFiles(IProgressMonitor monitor) throws JavaModelException, Exception {
		IPackageFragmentRoot ipfr = InitProFrameWorkAction.javaProject
				.findPackageFragmentRoot(new Path("/"
						+ InitProFrameWorkAction.projectProperties.getProjectName()
						+ "/"
						+ InitProFrameWorkAction.projectProperties.getJavaSourcesPath()[0]));

		//BaseDAO.java
		String orm = page1.getFws().get(Constant.ORM);		
		if(orm.equals(Constant.ORM_IBATIS)){
			//ibatis
			loadFilesBaseDAO(ipfr);
		}else{
			//hbm
			loadFilesBaseDAOhbm(ipfr);
		}	
		//BaseAction.java
		String mvc = page1.getFws().get(Constant.MVC);	
		if(mvc.equals(Constant.MVC_STRUTS2)){
			loadFilesBaseAction2(ipfr);
		}
	}
	private void loadFilesBaseDAO(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".common.dao";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/BaseDao.vm";
		outFilePath = srcPath +"/BaseDao.java";
		parameters.put("packageStr", packageStr);
		parameters.put("dataSourceSize", page2.getInitProDataSourceVoMap().size());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadFilesBaseDAOhbm(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".common.dao";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/BaseDao.hbm.vm";
		outFilePath = srcPath +"/BaseDao.java";
		parameters.put("packageStr", packageStr);
		parameters.put("dataSourceSize", page2.getInitProDataSourceVoMap().size());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadFilesBaseAction2(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".common.action";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/BaseAction2.vm";
		outFilePath = srcPath +"/BaseAction.java";
		parameters.put("packageStr", packageStr);
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadPageBase(String srcPath,IFolder ifolder,IProgressMonitor monitor) throws Exception{
		Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
		
		Enumeration enu = bundle.getEntryPaths(srcPath);
		while(enu.hasMoreElements()){
			String str=(String)enu.nextElement();
			URL url = bundle.getResource(str);
			try {
				String fileName = str.replaceAll(srcPath, "");
				if(fileName.endsWith("/")){
					IFolder cifolder = ifolder.getFolder(fileName);
					if(!cifolder.exists()){
						cifolder.create(true, true, monitor);
					}
					loadPageBase(str,cifolder,monitor);
				}else{
					InputStream is = url.openStream();	
					
					IFile ifile = ifolder.getFile(fileName);
					if(!ifile.exists()){
						ifile.create(is, true, monitor);
					}	
					
					is.close();
				}			
			} catch (Exception e) {
				e.printStackTrace();
			}		
		}		
	}
	//生成SYS_MENU功能
	private void loadSysMenu(IProgressMonitor monitor) throws Exception{
		IPackageFragmentRoot ipfr = InitProFrameWorkAction.javaProject
		.findPackageFragmentRoot(new Path("/"
				+ InitProFrameWorkAction.projectProperties.getProjectName()
				+ "/"
				+ InitProFrameWorkAction.projectProperties.getJavaSourcesPath()[0]));
		String orm = page1.getFws().get(Constant.ORM);		
		if(orm.equals(Constant.ORM_IBATIS)){
			//sqlmap
			loadSysMenuSqlMap(ipfr);
			//po
			loadSysMenuPo(ipfr);
			//daoimpl
			loadSysMenuDaoImpl(ipfr);
			//dbscript
			loadDbscript(monitor);
		}else{
			//hbm
			loadSysMenuHbm(ipfr);
			//po
			loadSysMenuHbmPo(ipfr);
			//daoimpl
			loadSysMenuDaoImplHbm(ipfr);
		}	
		String mvc = page1.getFws().get(Constant.MVC);
		//springMVC config
		if(mvc.equals(Constant.MVC_STRUTS)){
			
		}else if(mvc.equals(Constant.MVC_STRUTS2)){
			//web
			loadSysMenuWebAction2(ipfr);
		}else{
			//web
			loadSysMenuWeb(ipfr);
		}
		//dao
		loadSysMenuDao(ipfr);		
		//service
		loadSysMenuService(ipfr);
		//serviceimpl
		loadSysMenuServiceImpl(ipfr);		
		//page
		loadSysMenuPage(monitor);		
	}
	private void loadSysMenuSqlMap(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.orm.sqlmap";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		Map<String,InitProDataSourceVo> ds = page2.getInitProDataSourceVoMap();
		if(ds != null && ds.size() > 0){
			for(String key : ds.keySet()){
				InitProDataSourceVo initProDataSourceVo = ds.get(key);
				if(initProDataSourceVo.isDefault()){
					if(Constant.DB_TYPE_MYSQL.equals(initProDataSourceVo.getDataBaseType())){
						templateFile = "templates/initproframework/sysmenu/SysMenu.MySql.xml.vm";
					}
					if(Constant.DB_TYPE_ORACLE.equals(initProDataSourceVo.getDataBaseType())){
						templateFile = "templates/initproframework/sysmenu/SysMenu.Oracle.xml.vm";
					}
				}
			}
		}
				
		outFilePath = srcPath +"/SysMenu.xml";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuHbm(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.orm.hbm";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/SysMenu.hbm.xml.vm";
				
		outFilePath = srcPath +"/SysMenu.hbm.xml";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuPo(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.orm.po";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/SysMenu.vm";
		outFilePath = srcPath +"/SysMenu.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuHbmPo(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.orm.po";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/SysMenu.hbm.vm";
		outFilePath = srcPath +"/SysMenu.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuDao(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.dao";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/ISysMenuDao.vm";
		outFilePath = srcPath +"/ISysMenuDao.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuDaoImpl(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.dao.impl";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/SysMenuDaoImpl.vm";
		outFilePath = srcPath +"/SysMenuDaoImpl.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuDaoImplHbm(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.dao.impl";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/SysMenuDaoImpl.hbm.vm";
		outFilePath = srcPath +"/SysMenuDaoImpl.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuService(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.service";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/ISysMenuService.vm";
		outFilePath = srcPath +"/ISysMenuService.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuServiceImpl(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.service.impl";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/SysMenuServiceImpl.vm";
		outFilePath = srcPath +"/SysMenuServiceImpl.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuWeb(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.web";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/SysMenuController.vm";
		outFilePath = srcPath +"/SysMenuController.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuWebAction2(IPackageFragmentRoot ipfr) throws Exception{		
		loadSysMenuWebAction2Action(ipfr);
		loadSysMenuWebAction2Abstract(ipfr);
	}
	private void loadSysMenuWebAction2Action(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.web";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			ipf = ipfr.createPackageFragment(packageStr, false, null);
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/SysMenuAction.vm";
		outFilePath = srcPath +"/SysMenuAction.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuWebAction2Abstract(IPackageFragmentRoot ipfr) throws Exception{
		String templateFile = null;
		String outFilePath = null;
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		String packageStr = "com."+InitProFrameWorkAction.projectProperties.getProjectName()+".sys.web.abstracts";
		IPackageFragment ipf = ipfr.getPackageFragment(packageStr);
		if(!ipf.exists()){
			try {
				ipf = ipfr.createPackageFragment(packageStr, true, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}		

		String srcPath = InitProFrameWorkAction.projectProperties.getProjectParentPath() + ipf.getPath().toString();
		
		templateFile = "templates/initproframework/sysmenu/AbstractSysMenuAction.vm";
		outFilePath = srcPath +"/AbstractSysMenuAction.java";
		parameters.put("projectName", InitProFrameWorkAction.projectProperties.getProjectName());
		
		VelocityUtils.evaluate(templateFile, outFilePath, parameters);
	}
	private void loadSysMenuPage(IProgressMonitor monitor)throws Exception{
		IFolder ifolder = InitProFrameWorkAction.javaProject.getProject().getFolder("/" + InitProFrameWorkAction.projectProperties.getWebappPath());
		loadPageBase(Constant.SYSMENU_BASE,ifolder,monitor);
	}
	private void loadDbscript(IProgressMonitor monitor)throws Exception{
		IFile ifile = null;
		IFolder ifolder = InitProFrameWorkAction.javaProject.getProject().getFolder("/dbscript");
		Map<String,InitProDataSourceVo> ds = page2.getInitProDataSourceVoMap();
		if(ds != null && ds.size() > 0){
			for(String key : ds.keySet()){
				InitProDataSourceVo initProDataSourceVo = ds.get(key);
				if(initProDataSourceVo.isDefault()){
					if(Constant.DB_TYPE_MYSQL.equals(initProDataSourceVo.getDataBaseType())){
						loadPageBase("sources/dbscript/mysql/",ifolder,monitor);
						ifile = ifolder.getFile("DBScript_MySql.sql");
					}
					if(Constant.DB_TYPE_ORACLE.equals(initProDataSourceVo.getDataBaseType())){
						loadPageBase("sources/dbscript/oracle/",ifolder,monitor);
						ifile = ifolder.getFile("DBScript_Oracle.sql");
					}
				}
			}
		}		
		if(ifile != null && ifile.exists()){
			openFile(ifile);
		}
	}
	private void openFile(final IFile file){
		getShell().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				try {
					IDE.openEditor(page, file, true);
				} catch (PartInitException e) {
				}
			}
		});
	}
	/**
	 * We will accept the selection in the workbench to see if
	 * we can initialize from it.
	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
	
	@Override
	public boolean canFinish() {
		if(this.page2.getInitProDataSourceVoMap().size() < 1){
			return false;
		}
		return super.canFinish();
	}
}